package com.ruoyi.web.controller.hospital;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.hospital.*;
import com.ruoyi.common.utils.*;
import com.ruoyi.hospital.service.IMrInLabService;
import com.ruoyi.hospital.service.impl.MrOutDataServiceImpl;
import com.ruoyi.system.service.LsYySqlServerService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.MrInDataUtils.mrInDataToXML;

@RestController
@RequestMapping("/hospital/xml")
public class XmlController extends BaseController {

    @Autowired
    IMrInLabService mrInLabService;
    @Autowired
    LsYySqlServerService lsYySqlServerService;
    @Autowired
    MrOutDataServiceImpl mrOutDataService;

    @SneakyThrows
    @GetMapping(value = "/MrInLab")
    public AjaxResult getMrInLab(@RequestParam("fileName") String fileName) {
        System.out.println(fileName);
        fileName = "template/" + fileName;
        Resource resource = new ClassPathResource(fileName);
        Path filePath = resource.getFile().toPath();
        File inputFile = new File(filePath.toString());
        List<MrInLab> result = MrInLabUtils.getMrInLab(null);
        String onlyId = result.get(0).getOnlyId();
        mrInLabService.deleteMrInLab(onlyId);
        for (MrInLab mrInLab : result) {
            mrInLabService.saveMrInLab(mrInLab);
        }
        return success(result);
    }
    @SneakyThrows
    @GetMapping(value = "/MrInspection")
    public AjaxResult getMrInspection(@RequestParam("fileName") String fileName) {
        System.out.println(fileName);
        fileName = "template/" + fileName;
        Resource resource = new ClassPathResource(fileName);
        Path filePath = resource.getFile().toPath();
        File inputFile = new File(filePath.toString());
        List<MrInspection> result = MrInspectionUtils.getMrInspection(null);
        String onlyId = result.get(0).getOnlyId();
        mrInLabService.deleteMrInspection(onlyId);
        for (MrInspection mrInspection : result) {
            mrInLabService.saveMrInspection(mrInspection);
        }
        return success(result);
    }

    @SneakyThrows
    @GetMapping( "/MrInData")
    public AjaxResult getMrInData(ActPatList actPatList) {
//        System.out.println("patientId"+actPatList.getPatientId());
//        System.out.println("times"+actPatList.getInNums());
//        String mrIndata = lsYySqlServerService.getZyXmlStr(actPatList.getPatientId(),Integer.parseInt(actPatList.getInNums()));
//        String mrInLab = lsYySqlServerService.getMrInLab(actPatList.getPatientId(),Integer.parseInt(actPatList.getInNums()));
//        String mrInspection = lsYySqlServerService.getMrInspection(actPatList.getPatientId(),Integer.parseInt(actPatList.getInNums()));
//        System.out.println("mrIndata"+mrIndata);
//        System.out.println("mrInLab"+mrInLab);
//        System.out.println("mrInspection"+mrInspection);
        String mrIndata = "<mrInData>\n" +
                "  <mrInBaisc>\n" +
                "    <medicalClass>3</medicalClass>\n" +
                "    <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "    <onlyId>000347449800-1</onlyId>\n" +
                "    <organizationId>49455020-4</organizationId>\n" +
                "    <organization>日照市岚山区人民医院</organization>\n" +
                "    <mrId>376019</mrId>\n" +
                "    <idCard>230822195310120866</idCard>\n" +
                "    <name>宋井香</name>\n" +
                "    <inNumber>1</inNumber>\n" +
                "    <medicalPayment />\n" +
                "    <healthCard />\n" +
                "    <passport />\n" +
                "    <sex>2</sex>\n" +
                "    <birthday>1953-10-12T00:00:00</birthday>\n" +
                "    <ageYear>71</ageYear>\n" +
                "    <ageMonth />\n" +
                "    <ageDay />\n" +
                "    <nationality>CHN</nationality>\n" +
                "    <birthPlace>371102</birthPlace>\n" +
                "    <nativePlace>371102</nativePlace>\n" +
                "    <nation>01</nation>\n" +
                "    <occupation>27</occupation>\n" +
                "    <marriage>20</marriage>\n" +
                "    <administrativeDivision />\n" +
                "    <address>安东卫街道汾水村</address>\n" +
                "    <addrTelephone>15216339179</addrTelephone>\n" +
                "    <addrPostcode />\n" +
                "    <mobilePhone />\n" +
                "    <eMail />\n" +
                "    <register />\n" +
                "    <registerPlace />\n" +
                "    <registerPostcode />\n" +
                "    <workUnit />\n" +
                "    <unitTelephone />\n" +
                "    <unitPostcode />\n" +
                "    <linkmanCard />\n" +
                "    <linkman />\n" +
                "    <relationship />\n" +
                "    <linkmanAddr />\n" +
                "    <linkmanTele />\n" +
                "    <inWay />\n" +
                "    <inTime>2024-08-21T21:28:22</inTime>\n" +
                "    <inDepaId>1010500</inDepaId>\n" +
                "    <inDepa>骨科一组</inDepa>\n" +
                "    <standardSubject>04.03</standardSubject>\n" +
                "    <inStatus />\n" +
                "    <outWay />\n" +
                "    <beInDepa />\n" +
                "    <hod>0</hod>\n" +
                "  </mrInBaisc>\n" +
                "  <mrInDiagnosss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>6</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:50:39</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>T14.101</diagCode>\n" +
                "      <diagnoss>皮肤裂伤</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>9</diagOrder>\n" +
                "      <diagTime>2024-08-22T09:47:43</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>R91.x00x003</diagCode>\n" +
                "      <diagnoss>肺诊断性影像异常</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>1</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:49:59</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S52.801</diagCode>\n" +
                "      <diagnoss>桡骨骨折</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>2</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:50:06</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S52.201</diagCode>\n" +
                "      <diagnoss>尺骨骨折</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>3</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:50:14</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S42.100</diagCode>\n" +
                "      <diagnoss>肩胛骨骨折</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>4</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:50:23</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S22.300</diagCode>\n" +
                "      <diagnoss>肋骨骨折</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>5</diagOrder>\n" +
                "      <diagTime>2024-08-21T21:50:30</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S06.000</diagCode>\n" +
                "      <diagnoss>脑震荡</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>7</diagOrder>\n" +
                "      <diagTime>2024-08-22T09:47:19</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S27.000</diagCode>\n" +
                "      <diagnoss>创伤性气胸</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "    <MrInDiagnoss>\n" +
                "      <diagOrder>8</diagOrder>\n" +
                "      <diagTime>2024-08-22T09:47:29</diagTime>\n" +
                "      <diagType>2</diagType>\n" +
                "      <diagCode>S27.301</diagCode>\n" +
                "      <diagnoss>肺挫伤</diagnoss>\n" +
                "      <diagTreatment />\n" +
                "    </MrInDiagnoss>\n" +
                "  </mrInDiagnosss>\n" +
                "  <mrInRecord>\n" +
                "    <depaId>1010500</depaId>\n" +
                "    <depaName>骨科一组</depaName>\n" +
                "    <inTime>20240821213247</inTime>\n" +
                "    <recordTime>20240821215625</recordTime>\n" +
                "    <representor />\n" +
                "    <relationship />\n" +
                "    <telephone />\n" +
                "    <address />\n" +
                "    <reliability />\n" +
                "    <chiefComplaints>头部、胸部、左上肢等处外伤后疼痛约2小时。</chiefComplaints>\n" +
                "    <illnessHistory>患者于入院前约2小时骑电动车被三轮车撞伤，伤及头部、胸部、左上肢等处，伤后即意识不清，数分钟转醒，不能回忆受伤经过，感伤处疼痛、肿胀并伴活动受限，患者伤后感胸闷，无恶心呕吐，无腹痛、腹胀。患者受伤后未做任何处理，为求进一步治疗急来我院就诊，急诊行X线及CT检查后以“左尺桡骨骨折”收入我院。患者现神志清，精神可，未进饮食，大、小便未解，体重无明显变化。</illnessHistory>\n" +
                "    <pastHistory>健康状况：既往身体健康。疾病史：否认高血压病史，否认冠心病史，否认糖尿病史，否认脑血管病史，否认精神病史，以往未患其他疾病。传染病史：否认肝炎史，否认结核传染病史及密切接触史。预防接种史：预防接种史随当地计划免疫。手术史：3年前因右股骨骨折手术治疗（具体不详）。输血史：否认输血史。过敏史：否认药物过敏史。</pastHistory>\n" +
                "    <personalHistory>生于原籍。无长期外地居住史。无工业毒物、粉尘及放射性物质接触史。平日生活规律，无吸烟嗜好，无饮酒嗜好。否认冶游史。</personalHistory>\n" +
                "    <maritalHistory>已婚，21岁结婚，配偶体健，已育，1子4女体健。</maritalHistory>\n" +
                "    <familyHistory>父亲已故，具体不详。母亲已故，具体不详。有兄弟、姐妹，均体健，家族中无与患者类似疾病。无遗传病史，无传染病史。</familyHistory>\n" +
                "    <medicalExamination />\n" +
                "    <accessoryExamination>检查日期;检查项目;检查结果;检查医院及编号;2024-08-21;新冠抗原检测;未做;2024-08-21;左肩、左尺桡骨DR;左尺桡骨骨折，左侧第5肋骨骨折，考虑左侧第3肋骨陈旧改变，请结合临床，必要时进一步检查。;本院;2024-08-21;颅脑、颈椎、胸部CT;左侧肩胛骨喙突骨折，左侧肋骨多发骨折，左侧气胸，左肺挫裂伤，双肺多发小结节灶，双肺局灶性炎症、纤维灶，心影略大、冠状动脉钙化，颈椎退行性变，颅脑CT平扫未见明显异常。;本院</accessoryExamination>\n" +
                "  </mrInRecord>\n" +
                "  </mrInData>";

        String mrInLab = "<mrInLabsData>\n" +
                "  <mrInLabs>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4657090</reportNo>\n" +
                "      <inspectTime>2024-08-22 09:19:37</inspectTime>\n" +
                "      <inspectCode>0403443</inspectCode>\n" +
                "      <inspectName>*梅毒螺旋体抗体</inspectName>\n" +
                "      <inspectResult>0.03</inspectResult>\n" +
                "      <measurementUnit>S/CO</measurementUnit>\n" +
                "      <referenceToplimit>1.0000</referenceToplimit>\n" +
                "      <referenceLowerlimit>0.0000</referenceLowerlimit>\n" +
                "      <referenceValue>0~1</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>陈丽冰</rapporteur>\n" +
                "      <checked>陈庆云</checked>\n" +
                "    </MrInLab>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4656704</reportNo>\n" +
                "      <inspectTime>2024-08-22 08:07:21</inspectTime>\n" +
                "      <inspectCode>0101030</inspectCode>\n" +
                "      <inspectName>平均血小板体积[MPV]</inspectName>\n" +
                "      <inspectResult>9.90</inspectResult>\n" +
                "      <measurementUnit>fL</measurementUnit>\n" +
                "      <referenceToplimit>13.5000</referenceToplimit>\n" +
                "      <referenceLowerlimit>6.8000</referenceLowerlimit>\n" +
                "      <referenceValue>6.8~13.5</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>张薇薇</rapporteur>\n" +
                "      <checked>杨晓东</checked>\n" +
                "    </MrInLab>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4656876</reportNo>\n" +
                "      <inspectTime>2024-08-22 09:02:32</inspectTime>\n" +
                "      <inspectCode>0301014</inspectCode>\n" +
                "      <inspectName>*谷草转氨酶[AST]</inspectName>\n" +
                "      <inspectResult>31.20</inspectResult>\n" +
                "      <measurementUnit>U/L</measurementUnit>\n" +
                "      <referenceToplimit>37.0000</referenceToplimit>\n" +
                "      <referenceLowerlimit>无</referenceLowerlimit>\n" +
                "      <referenceValue>≤32</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>杨晓东</rapporteur>\n" +
                "      <checked>张海英</checked>\n" +
                "    </MrInLab>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4656704</reportNo>\n" +
                "      <inspectTime>2024-08-22 08:07:21</inspectTime>\n" +
                "      <inspectCode>0101012</inspectCode>\n" +
                "      <inspectName>嗜碱性粒细胞计数[BAS#]</inspectName>\n" +
                "      <inspectResult>0.00</inspectResult>\n" +
                "      <measurementUnit>10^9/L</measurementUnit>\n" +
                "      <referenceToplimit>0.2000</referenceToplimit>\n" +
                "      <referenceLowerlimit>0.0000</referenceLowerlimit>\n" +
                "      <referenceValue>0~0.2</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>张薇薇</rapporteur>\n" +
                "      <checked>杨晓东</checked>\n" +
                "    </MrInLab>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4656876</reportNo>\n" +
                "      <inspectTime>2024-08-22 09:02:32</inspectTime>\n" +
                "      <inspectCode>0301021</inspectCode>\n" +
                "      <inspectName>*总胆固醇[CHO]</inspectName>\n" +
                "      <inspectResult>3.88</inspectResult>\n" +
                "      <measurementUnit>mmol/L</measurementUnit>\n" +
                "      <referenceToplimit>5.2000</referenceToplimit>\n" +
                "      <referenceLowerlimit>无</referenceLowerlimit>\n" +
                "      <referenceValue>≤5.2</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>杨晓东</rapporteur>\n" +
                "      <checked>张海英</checked>\n" +
                "    </MrInLab>\n" +
                "    <MrInLab>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>4656876</reportNo>\n" +
                "      <inspectTime>2024-08-22 09:02:32</inspectTime>\n" +
                "      <inspectCode>0301028</inspectCode>\n" +
                "      <inspectName>*尿素[UREA]</inspectName>\n" +
                "      <inspectResult>2.90</inspectResult>\n" +
                "      <measurementUnit>mmol/L</measurementUnit>\n" +
                "      <referenceToplimit>8.3000</referenceToplimit>\n" +
                "      <referenceLowerlimit>1.7000</referenceLowerlimit>\n" +
                "      <referenceValue>2.76~8.07</referenceValue>\n" +
                "      <inspectMethod />\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>检验科</inspectDepa>\n" +
                "      <rapporteur>杨晓东</rapporteur>\n" +
                "      <checked>张海英</checked>\n" +
                "    </MrInLab>\n" +
                "  </mrInLabs>\n" +
                "</mrInLabsData>";


        String mrInspection = "<mrInspectionsData>\n" +
                "  <mrInInspections>\n" +
                "    <MrInInspection>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>3552741</reportNo>\n" +
                "      <classification>01</classification>\n" +
                "      <inspectTime>2024-08-22 14:23:51</inspectTime>\n" +
                "      <inspectObjective>CT</inspectObjective>\n" +
                "      <inspectCode>002374;002411;002490;002514</inspectCode>\n" +
                "      <inspectName>颅脑CT平扫（64排）;骨盆CT平扫(64排);左侧尺桡骨CT平扫+三维重建（64排）;胸部、上腹部CT平扫+肋骨重建</inspectName>\n" +
                "      <inspectPosition>左尺桡骨上腹胸部肋骨骨盆颅脑</inspectPosition>\n" +
                "      <inspectMethod>CT</inspectMethod>\n" +
                "      <inspectProcess />\n" +
                "      <inspectDepiction>脑实质未见明显异常密度影，双侧脑沟、脑裂、脑池大小、形态、密度未见明显异常；脑室系统未见明显异常；中线结构居中，颅骨骨质未见明显异常。\n" +
                "胸廓对称，气管纵隔居中，左侧胸腔见游离气体密度影，较前2024-08-21减少；左肺见多发斑片状磨玻璃影，边界欠清，较前减少，双肺见多发结节，部分边界较清，较前变化不明显，双肺见散在条片状高密度影；纵隔未见肿大淋巴结或肿块影，心影形态略大，冠状动脉走行区见致密影；双侧胸膜未见肥厚、粘连，双侧胸腔内见少量液体密度影。左侧肩胛骨喙突见骨折裂隙，断端对位可。\n" +
                "肋骨重建示：左侧第5、6、7肋骨见骨折裂隙，断端略错位；左侧第3、4肋骨欠规整，边缘硬化。\n" +
                "肝实质密度均匀，未见局灶性异常密度影，肝内外胆管未见扩张，脾不大，实质密度均匀；胆囊不大，壁规整，其内见类圆形高密度；胰腺大小形态及密度未见明显异常，腹膜后未见明显肿大淋巴结影。左侧肾上腺区见类圆形低密度，边界较清，直径约为20.0mm。腰背部见类圆形脂肪密度影，直径约为28.0mm。\n" +
                "骨盆诸组成骨骨质结构完整，皮质连续，双侧髋关节及骶髂关节间隙未见明显异常，未见明显骨折及脱位征象；其周围软组织未见明显肿胀。\n" +
                "左侧尺桡骨中段见裂隙，断端略错位，周围软组织肿胀。\n" +
                "</inspectDepiction>\n" +
                "      <inspectResult>左侧肩胛骨喙突骨折、左侧肋骨多发骨折复查所见左侧尺桡骨骨折并周围软组织肿胀\n" +
                "左侧肋骨陈旧性改变\n" +
                "左侧气胸、左肺挫伤、双侧少量胸腔积液复查所见\n" +
                "双肺多发小结节灶复查所见，建议年度复查\n" +
                "双肺局灶性炎症、纤维灶、心影略大、冠状动脉钙化\n" +
                "胆囊结石、腰背部皮下脂肪瘤\n" +
                "左侧肾上腺区占位，请结合临床及实验室检查，必要时结合上腹部增强CT检查\n" +
                "颅脑、骨盆CT平扫未见明显异常\n" +
                "注：因许多骨折非常细微，单次检查并不一定能准确计数，请行多次检查并对照（至少应于外伤时及外伤后3-4周做两次肋骨扫描）\n" +
                "</inspectResult>\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>CT室</inspectDepa>\n" +
                "      <rapporteur>何成顺</rapporteur>\n" +
                "      <checked>王杰</checked>\n" +
                "      <cloudBrowsingURL>http://11.227.0.14:8034/XTechReportWeb/ReportHtml/PDFPAGE.aspx?RepNo=3552741&amp;RepTypeNo=CT</cloudBrowsingURL>\n" +
                "    </MrInInspection>\n" +
                "    <MrInInspection>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>3552753</reportNo>\n" +
                "      <classification>04</classification>\n" +
                "      <inspectTime>2024-08-22 14:21:40</inspectTime>\n" +
                "      <inspectObjective>DR</inspectObjective>\n" +
                "      <inspectCode>002259;002297</inspectCode>\n" +
                "      <inspectName>左手正斜位(DR);左侧肘关节正侧位(DR)</inspectName>\n" +
                "      <inspectPosition>左手正斜位(DR);左侧肘关节正侧位(DR)</inspectPosition>\n" +
                "      <inspectMethod>DR</inspectMethod>\n" +
                "      <inspectProcess />\n" +
                "      <inspectDepiction>受限体位\n" +
                "左尺桡骨骨折复查：尺桡骨骨折断端错位，骨折线清，见外固定物影，余骨及关节未见明显异常征象。\n" +
                "左手组成骨骨质结构完整，骨皮质连续，未见明显骨折征象，关节间隙可。\n" +
                "</inspectDepiction>\n" +
                "      <inspectResult>左尺桡骨骨折复查表现\n" +
                "左手骨质未见明显异常\n" +
                "</inspectResult>\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>DR室</inspectDepa>\n" +
                "      <rapporteur>厉娜娜</rapporteur>\n" +
                "      <checked>郑军</checked>\n" +
                "      <cloudBrowsingURL>http://11.227.0.14:8034/XTechReportWeb/ReportHtml/PDFPAGE.aspx?RepNo=3552753&amp;RepTypeNo=DR</cloudBrowsingURL>\n" +
                "    </MrInInspection>\n" +
                "    <MrInInspection>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>3556240</reportNo>\n" +
                "      <classification>01</classification>\n" +
                "      <inspectTime>2024-08-26 08:58:56</inspectTime>\n" +
                "      <inspectObjective>CT</inspectObjective>\n" +
                "      <inspectCode>002391</inspectCode>\n" +
                "      <inspectName>胸部CT平扫(64排)</inspectName>\n" +
                "      <inspectPosition>胸部CT平扫（64排）</inspectPosition>\n" +
                "      <inspectMethod>CT</inspectMethod>\n" +
                "      <inspectProcess />\n" +
                "      <inspectDepiction>胸廓对称，气管纵隔居中，前次（2024-08-22）所见左侧胸腔游离气体密度影今次未见；左肺见多发斑片状磨玻璃影，边界欠清，较前减少，双肺见多发结节，部分边界较清，较前变化不明显，双肺见散在条片状高密度影；纵隔未见肿大淋巴结或肿块影，心影形态略大，冠状动脉走行区见致密影；双侧胸膜未见肥厚、粘连，双侧胸腔内见液体密度影，左侧较前增多。左侧肩胛骨喙突见骨折裂隙，断端对位可。左侧多发肋骨见骨折裂隙，断端略错位；左侧第3、4肋骨欠规整，边缘硬化。\n" +
                "</inspectDepiction>\n" +
                "      <inspectResult>左侧肩胛骨喙突骨折、左侧肋骨多发骨折复查所见\n" +
                "左侧肋骨陈旧性改变\n" +
                "左侧气胸、左肺挫伤、双侧胸腔积液复查所见\n" +
                "双肺多发小结节灶复查所见，建议年度复查\n" +
                "双肺局灶性炎症、纤维灶、心影略大、冠状动脉钙化\n" +
                "\n" +
                "\n" +
                "</inspectResult>\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>CT室</inspectDepa>\n" +
                "      <rapporteur>马良勇</rapporteur>\n" +
                "      <checked>顾洪升</checked>\n" +
                "      <cloudBrowsingURL>http://11.227.0.14:8034/XTechReportWeb/ReportHtml/PDFPAGE.aspx?RepNo=3556240&amp;RepTypeNo=CT</cloudBrowsingURL>\n" +
                "    </MrInInspection>\n" +
                "    <MrInInspection>\n" +
                "      <medicalClass>3</medicalClass>\n" +
                "      <mdtrtsn>000347449800-1</mdtrtsn>\n" +
                "      <onlyId>000347449800-1</onlyId>\n" +
                "      <organizationId>49455020-4</organizationId>\n" +
                "      <organization>日照市岚山区人民医院</organization>\n" +
                "      <mrId>376019</mrId>\n" +
                "      <orderNo>0</orderNo>\n" +
                "      <reportNo>3557335</reportNo>\n" +
                "      <classification>04</classification>\n" +
                "      <inspectObjective>DR</inspectObjective>\n" +
                "      <inspectCode>011275</inspectCode>\n" +
                "      <inspectName>医用干式激光胶片胶片11×14</inspectName>\n" +
                "      <inspectPosition />\n" +
                "      <inspectMethod>DR</inspectMethod>\n" +
                "      <inspectProcess />\n" +
                "      <inspectDepiction>胶片费\n" +
                "</inspectDepiction>\n" +
                "      <inspectResult>\n" +
                "</inspectResult>\n" +
                "      <inspectUnit>日照市岚山区人民医院</inspectUnit>\n" +
                "      <inspectDepa>DR室</inspectDepa>\n" +
                "      <rapporteur>刘向华</rapporteur>\n" +
                "      <checked>郑军</checked>\n" +
                "      <cloudBrowsingURL>http://11.227.0.14:8034/XTechReportWeb/ReportHtml/PDFPAGE.aspx?RepNo=3557335&amp;RepTypeNo=DR</cloudBrowsingURL>\n" +
                "    </MrInInspection>\n" +
                "  </mrInInspections>\n" +
                "</mrInspectionsData>";
        if(StringUtils.isNull(mrIndata)){
            return error("患者住院基础数据为空请联系管理员");
        }
        Map<Object, Object> map = new HashMap<>();

        MrInData mrInDataResult = MrInDataUtils.getMrInData(mrIndata);

        String onlyId = mrInDataResult.getMrInBaisc().getOnlyId();
        mrInLabService.delleteMrInData(onlyId);
        new Thread(() -> {
            mrInLabService.saveMrInData(mrInDataResult);
        }).start();
        map.put("mrInData",mrInDataResult);
        if(StringUtils.isNotNull(mrInLab)){
            List<MrInLab> mrInLabResult = MrInLabUtils.getMrInLab(mrInLab);
            System.out.println(mrInLabResult);
            mrInLabService.deleteMrInLab(onlyId);
            new Thread(() -> {
                for (MrInLab mrInLabData : mrInLabResult) {
                    mrInLabService.saveMrInLab(mrInLabData);
                }
            }).start();
            map.put("mrInLab",mrInLabResult);
        }else{
            map.put("mrInLab",null);
        }
        if(StringUtils.isNotNull(mrInspection)){
            List<MrInspection> MrInspectionResult = MrInspectionUtils.getMrInspection(mrInspection);
            mrInLabService.deleteMrInspection(onlyId);
            new Thread(() -> {
                for (MrInspection mrInspectionData : MrInspectionResult) {
                    mrInLabService.saveMrInspection(mrInspectionData);
                }
            }).start();
            map.put("mrInspection",MrInspectionResult);
        }else{
            map.put("mrInspection",null);
        }
        return success(map);
    }

    @GetMapping( "/update")
    public AjaxResult updateXml(MrInBaisc mrInBaisc) throws Exception {
        mrInLabService.deleteMrInBaisc(mrInBaisc.getOnlyId());
        mrInLabService.saveMrInBaisc(mrInBaisc);

        //根据onlyId查询住院检查等信息
        MrInData mrInData = mrInLabService.queryByOnlyId(mrInBaisc.getOnlyId());
        List<MrInLab> mrInLabList = mrInLabService.queryMrInLab(mrInBaisc.getOnlyId());
        List<MrInspection> mrInspectionList = mrInLabService.queryMrInspection(mrInBaisc.getOnlyId());

        //将住院信息转为xml存储过程
        String mrInDataXml = MrInDataUtils.mrInDataToXML(mrInData);
        System.out.println(mrInDataXml);
        String mrInLabXml = null;
        if(!mrInLabList.isEmpty()){
            mrInLabXml = MrInLabUtils.mrInLabToXML(mrInLabList);
            System.out.println(mrInLabXml);
        }
        String mrInspectionXml = null;
        if(!mrInspectionList.isEmpty()){
            mrInspectionXml = MrInspectionUtils.mrInspectionToXML(mrInspectionList);
            System.out.println(mrInspectionXml);
        }

        //将存储过程加密
        String mrInDataSm4 = SM4Util.sm4EncryptUtil(mrInDataXml);
        System.out.println("mrInDataSm4" + mrInDataSm4);

        String mrInLabSm4 = null;
        if(StringUtils.isNotNull(mrInLabXml)){
            mrInLabSm4 = SM4Util.sm4EncryptUtil(mrInLabXml);
            System.out.println("mrInLabSm4" + mrInLabSm4);
        }

        String mrInspectionSm4 = null;
        if(StringUtils.isNotNull(mrInspectionXml)){
            mrInspectionSm4 = SM4Util.sm4EncryptUtil(mrInspectionXml);
            System.out.println("mrInspectionSm4" + mrInspectionSm4);
        }



        //往接口发送数据
        String finalPatName = mrInBaisc.getName();
        String finalMrInLabSm = mrInLabSm4;
        String finalMrInspectionSm = mrInspectionSm4;
        new Thread(() -> {
            Map json = POSTUtil.sendMrInData(mrInDataSm4,MsgType.mrInAcc);
//            String json = "<message>\n" +
//                    "<msg_type>031</msg_type>\n" +
//                    "<msg_sender>650008999</msg_sender>\n" +
//                    "<msg_orgsn>101</msg_orgsn>\n" +
//                    "<msg_time>63680896479793</msg_time>\n" +
//                    "<msg_rspcode>01</msg_rspcode>\n" +
//                    "<msg_rspdesc>报文体加密格式错误</msg_rspdesc>\n" +
//                    "<msg_sign>0</msg_sign>\n" +
//                    "<msg_body></msg_body>\n" +
//                    "</message>";
            PostResult postResult = null;
            try {
                postResult = PostReturnUtils.postResult(json.get("json").toString());
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            postResult.setPatName(finalPatName);
            postResult.setRucan(json.get("msg").toString());
            mrOutDataService.saveLog(postResult);
            if(StringUtils.isNotNull(finalMrInLabSm)){
                Map labJson = POSTUtil.sendMrInData(finalMrInLabSm,MsgType.mrInLabAcc);
                PostResult labResult = null;
                try {
                    labResult = PostReturnUtils.postResult(json.get("json").toString());
                } catch (JAXBException e) {
                    e.printStackTrace();
                }
                labResult.setPatName(finalPatName);
                labResult.setRucan(labJson.get("msg").toString());
                mrOutDataService.saveLog(labResult);
            }
            if (StringUtils.isNotNull(finalMrInspectionSm)) {
                Map inspectionJson = POSTUtil.sendMrInData(finalMrInspectionSm,MsgType.mrInspectionAcc);
                PostResult inspectionResult = null;
                try {
                    inspectionResult = PostReturnUtils.postResult(inspectionJson.get("json").toString());
                } catch (JAXBException e) {
                    e.printStackTrace();
                }
                inspectionResult.setPatName(finalPatName);
                inspectionResult.setRucan(inspectionJson.get("msg").toString());
                mrOutDataService.saveLog(inspectionResult);
            }
        }).start();
        return success("上传成功");
    }

    @GetMapping( "/updateMrInData")
    public AjaxResult updateMrInData(MrInBaisc mrInBaisc) throws Exception {
        mrInLabService.deleteMrInBaisc(mrInBaisc.getOnlyId());
        mrInLabService.saveMrInBaisc(mrInBaisc);

        //根据onlyId查询住院检查等信息
        MrInData mrInData = mrInLabService.queryByOnlyId(mrInBaisc.getOnlyId());

        //将住院信息转为xml存储过程
        String mrInDataXml = MrInDataUtils.mrInDataToXML(mrInData);
        System.out.println(mrInDataXml);

        //将存储过程加密
        String mrInDataSm4 = SM4Util.sm4EncryptUtil(mrInDataXml);
        System.out.println("mrInDataSm4" + mrInDataSm4);
        //往接口发送数据
        Map json = POSTUtil.sendMrInData(mrInDataSm4,MsgType.mrInAcc);
        PostResult postResult = null;
        postResult = PostReturnUtils.postResult(json.get("json").toString());
        postResult.setPatName(mrInBaisc.getName());
        postResult.setRucan(json.get("msg").toString());
        mrOutDataService.saveLog(postResult);
        return success(postResult.getMsgRspdesc());
    }

    @GetMapping( "/updateMrInLab")
    public AjaxResult updateMrInLab(MrInBaisc mrInBaisc) throws Exception {
        //根据onlyId查询住院检查等信息
        List<MrInLab> mrInLabList = mrInLabService.queryMrInLab(mrInBaisc.getOnlyId());

        if(mrInLabList.isEmpty()){
            return success("实验室检验数据为空");
        }

        //将住院信息转为xml存储过程
        String mrInLabXml = MrInLabUtils.mrInLabToXML(mrInLabList);
        System.out.println(mrInLabXml);

        //将存储过程加密
        String mrInLabSm4 = SM4Util.sm4EncryptUtil(mrInLabXml);
        System.out.println("mrInLabSm4" + mrInLabSm4);
        //往接口发送数据
        Map json = POSTUtil.sendMrInData(mrInLabSm4,MsgType.mrInLabAcc);
        PostResult postResult = null;
        postResult = PostReturnUtils.postResult(json.get("json").toString());
        postResult.setPatName(mrInBaisc.getName());
        postResult.setRucan(json.get("msg").toString());
        mrOutDataService.saveLog(postResult);
        return success(postResult.getMsgRspdesc());
    }

    @GetMapping( "/updateMrInspection")
    public AjaxResult updateMrInspection(MrInBaisc mrInBaisc) throws Exception {
        //根据onlyId查询住院检查等信息
        List<MrInspection> mrInspectionList = mrInLabService.queryMrInspection(mrInBaisc.getOnlyId());

        if(mrInspectionList.isEmpty()){
            return success("实验室检验数据为空");
        }

        //将住院信息转为xml存储过程
        String mrInspectionXml = MrInspectionUtils.mrInspectionToXML(mrInspectionList);
        System.out.println(mrInspectionXml);

        //将存储过程加密
        String mrInspectionSm4 = SM4Util.sm4EncryptUtil(mrInspectionXml);
        System.out.println("mrInspectionSm4" + mrInspectionSm4);
        //往接口发送数据
        Map json = POSTUtil.sendMrInData(mrInspectionSm4,MsgType.mrInspectionAcc);
        PostResult postResult = null;
        postResult = PostReturnUtils.postResult(json.get("json").toString());
        postResult.setPatName(mrInBaisc.getName());
        postResult.setRucan(json.get("msg").toString());
        mrOutDataService.saveLog(postResult);
        return success(postResult.getMsgRspdesc());
    }

}

